/*
 * June 15, 2009
 *
 * Image sources:
 * http://www.alaska-in-pictures.com/data/homepage/19/autumn-arctic-landscape_7906.jpg
 * http://s.decofinder.com/0/1/vig-mos/vig/497/497404/Landscape-Decoration-Blanc-Carrare.jpg
 * http://www.creativehotsauce.com/images/landscape-design.jpg
 */
package app;

import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import javax.microedition.io.Connector;
import javax.microedition.io.PushRegistry;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import org.netbeans.microedition.lcdui.SplashScreen;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;

/**
 * @author thirdy
 */
public class ThesisMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private ThesisServer thesisServer;
    private ThesisClient thesisClient;
    private ThesisRMS thesisSrvrSettings;
    private ThesisRMS thesisClntSettings;
    private boolean[] srvrSettingsFlag = new boolean[ThesisConstants.SETTINGS_FLAG_SIZE];
    private String fileNamePrefix = "ths";
    private boolean saveRecsFlag = false;
    private boolean pushRegFlag = false;
    private boolean[] clntSettingsFlag = new boolean[1];
    private String defaultSrvrNum = "";
    private String clientNum = "";
    private boolean startedByReq = false;
    private MessageConnection SMSServer = null;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private List mainMenu;
    private Form thesisServerLog;
    private List thesisClientMenu;
    private TextBox serverNumber;
    private Alert thesisErrorAlert;
    private Alert thesisInfoAlert;
    private Form serverSettingsForm;
    private ChoiceGroup saveRecFlagCG;
    private TextField fnPrfxTextField;
    private ChoiceGroup pushRegCG;
    private TextField clientNumTF;
    private ChoiceGroup notificationsCG;
    private Form clientSettingsForm;
    private ChoiceGroup clntSettingsFlgCG;
    private TextField defSrvrTF;
    private Form BusyForm;
    private Gauge busyGauge;
    private Form autoModeForm;
    private Gauge timeIntervalGauge;
    private ChoiceGroup autoModeDurationCG;
    private TextField autoModeClientNumTF;
    private WaitScreen autoServerWaitScreen;
    private Command exitCommand;
    private Command Send;
    private Command backCommand;
    private Command cancelCommand;
    private Command saveCommand;
    private Command notifcnsCommand;
    private Command okCommand;
    private Command settingsCommand;
    private Command autoModeCommand;
    private Command exitCommand1;
    private Image serverIcon;
    private Image splashImg;
    private Image clientIcon;
    private Image notifcIcon;
    private Image exitIcon;
    private Image errorIcon;
    private Image downldIcon;
    private Image infoIcon;
    private SimpleCancellableTask autoServerTask;
    private Image image2;
    private Image image1;
    private Image image3;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The ThesisMIDlet constructor.
     */
    public ThesisMIDlet() {
        try {
            thesisSrvrSettings = new ThesisRMS(ThesisConstants.RMS_SRVR_SETTINGS_NAME);
            thesisClntSettings = new ThesisRMS(ThesisConstants.RMS_CLNT_SETTINGS_NAME);
            loadSrvrSettings();
            loadClntSettings();
        } catch (Throwable ex) {
            showErrorAlert(ex);
        }
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|
    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        // check if started by a request via push reg
        if (pushRegFlag) {
            try {
                SMSServer = (MessageConnection) Connector.open("sms://:" + ThesisConstants.SMSPORT);
                SMSServer.setMessageListener(new MessageListener() {

                    public void notifyIncomingMessage(MessageConnection conn) {
                        startedByReq = true;
                    }
                });
                // wait for the req
                Thread.sleep(1000);
            } catch (Throwable ex) {
                showErrorAlert(ex);
            }
            if (startedByReq) {
                switchDisplayable(null, getThesisServerLog());
                initServer();
                return;
            } else {
                try {
                    SMSServer.close();
                } catch (Throwable ex) {
                    showErrorAlert(ex);
                }
            }
        }
        switchDisplayable(null, getMainMenu());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
        switchDisplayable(null, getMainMenu());//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == autoModeForm) {//GEN-BEGIN:|7-commandAction|1|180-preAction
            if (command == cancelCommand) {//GEN-END:|7-commandAction|1|180-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisServerLog());//GEN-LINE:|7-commandAction|2|180-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|3|188-preAction
                // write pre-action user code here
                switchDisplayable(null, getAutoServerWaitScreen());//GEN-LINE:|7-commandAction|4|188-postAction
                // write post-action user code here
                autoTaskRun = true;
                String autoClientNum = getAutoModeClientNumTF().getString();
                if (autoClientNum.startsWith("0")) {
                autoClientNum = "+63" + autoClientNum.substring(1);
                }
                thesisServer.setClientNum(autoClientNum);
            }//GEN-BEGIN:|7-commandAction|5|191-preAction
        } else if (displayable == autoServerWaitScreen) {
            if (command == WaitScreen.FAILURE_COMMAND) {//GEN-END:|7-commandAction|5|191-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|6|191-postAction
                // write post-action user code here
            } else if (command == WaitScreen.SUCCESS_COMMAND) {//GEN-LINE:|7-commandAction|7|190-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|8|190-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|9|204-preAction
                // write pre-action user code here
                autoTaskRun = false;
                switchDisplayable(null, getAutoModeForm());//GEN-LINE:|7-commandAction|10|204-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|11|201-preAction
                // write pre-action user code here
                autoTaskRun = false;
                switchDisplayable(null, getThesisServerLog());//GEN-LINE:|7-commandAction|12|201-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|134-preAction
        } else if (displayable == clientSettingsForm) {
            if (command == cancelCommand) {//GEN-END:|7-commandAction|13|134-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisClientMenu());//GEN-LINE:|7-commandAction|14|134-postAction
                // write post-action user code here
            } else if (command == saveCommand) {//GEN-LINE:|7-commandAction|15|133-preAction
                // write pre-action user code here
                // validate form
                if (getClntSettingsFlgCG().isSelected(0) && (getDefSrvrTF().getString() == null ||
                        getDefSrvrTF().getString().equals(ThesisConstants.EMPTY))) {
                    showErrorAlert("Enter default server number");
                    return;
                }
                saveClntSettings();
//GEN-LINE:|7-commandAction|16|133-postAction
                // write post-action user code here
                getThesisInfoAlert().setString(ThesisConstants.SETTINGS_SAVED_MSG);
                switchDisplayable(getThesisInfoAlert(), getThesisClientMenu());
            }//GEN-BEGIN:|7-commandAction|17|23-preAction
        } else if (displayable == mainMenu) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|17|23-preAction
                // write pre-action user code here
                mainMenuAction();//GEN-LINE:|7-commandAction|18|23-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|19|59-preAction
        } else if (displayable == serverNumber) {
            if (command == Send) {//GEN-END:|7-commandAction|19|59-preAction
                // write pre-action user code here
                String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                thesisClient.sendSMS(msg, serverNumber.getString());
//GEN-LINE:|7-commandAction|20|59-postAction
                // write post-action user code here
                switchDisplayable(null, getThesisClientMenu());
                getServerNumber().setString(ThesisConstants.EMPTY);
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|21|60-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisClientMenu());//GEN-LINE:|7-commandAction|22|60-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|23|119-preAction
        } else if (displayable == serverSettingsForm) {
            if (command == cancelCommand) {//GEN-END:|7-commandAction|23|119-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisServerLog());//GEN-LINE:|7-commandAction|24|119-postAction
                // write post-action user code here
            } else if (command == saveCommand) {//GEN-LINE:|7-commandAction|25|122-preAction
                // write pre-action user code here
                try {
                    saveSrvrSettings();
                } catch (Throwable t) {
                    showErrorAlert(t);
                    return;
                }
//GEN-LINE:|7-commandAction|26|122-postAction
                // write post-action user code here
                getThesisInfoAlert().setString(ThesisConstants.SETTINGS_SAVED_MSG);
                switchDisplayable(getThesisInfoAlert(), getThesisServerLog());
            }//GEN-BEGIN:|7-commandAction|27|44-preAction
        } else if (displayable == thesisClientMenu) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|27|44-preAction
                // write pre-action user code here
                thesisClientMenuAction();//GEN-LINE:|7-commandAction|28|44-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|29|51-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|30|51-postAction
                // write post-action user code here
            } else if (command == notifcnsCommand) {//GEN-LINE:|7-commandAction|31|127-preAction
                // write pre-action user code here
                switchDisplayable(null, getClientSettingsForm());//GEN-LINE:|7-commandAction|32|127-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|33|177-preAction
        } else if (displayable == thesisServerLog) {
            if (command == autoModeCommand) {//GEN-END:|7-commandAction|33|177-preAction
                // write pre-action user code here
                getAutoModeClientNumTF().setString(clientNum);
                switchDisplayable(null, getAutoModeForm());//GEN-LINE:|7-commandAction|34|177-postAction
                // write post-action user code here
            } else if (command == exitCommand) {//GEN-LINE:|7-commandAction|35|41-preAction
                // write pre-action user code here
                switchDisplayable(null, getMainMenu());//GEN-LINE:|7-commandAction|36|41-postAction
                // write post-action user code here
                getThesisServerLog().deleteAll();
            } else if (command == settingsCommand) {//GEN-LINE:|7-commandAction|37|161-preAction
                // write pre-action user code here
                loadSrvrSettings();
                switchDisplayable(null, getServerSettingsForm());//GEN-LINE:|7-commandAction|38|161-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|39|7-postCommandAction
        }//GEN-END:|7-commandAction|39|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|40|
    //</editor-fold>//GEN-END:|7-commandAction|40|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: mainMenu ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of mainMenu component.
     * @return the initialized component instance
     */
    public List getMainMenu() {
        if (mainMenu == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            mainMenu = new List("Main menu", Choice.IMPLICIT);//GEN-BEGIN:|22-getter|1|22-postInit
            mainMenu.append("Start as server", getServerIcon());
            mainMenu.append("Start as client", getClientIcon());
            mainMenu.append("Exit", getExitIcon());
            mainMenu.setCommandListener(this);
            mainMenu.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return mainMenu;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: mainMenuAction ">//GEN-BEGIN:|22-action|0|22-preAction
    /**
     * Performs an action assigned to the selected list element in the mainMenu component.
     */
    public void mainMenuAction() {//GEN-END:|22-action|0|22-preAction
        // enter pre-action user code here
        String __selectedString = getMainMenu().getString(getMainMenu().getSelectedIndex());//GEN-BEGIN:|22-action|1|26-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Start as server")) {//GEN-END:|22-action|1|26-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisServerLog());//GEN-LINE:|22-action|2|26-postAction
                // write post-action user code here
                initServer();
            } else if (__selectedString.equals("Start as client")) {//GEN-LINE:|22-action|3|27-preAction
                // write pre-action user code here
                switchDisplayable(null, getThesisClientMenu());//GEN-LINE:|22-action|4|27-postAction
                // write post-action user code here
                cleanConnections();
                thesisClient = new ThesisClient(this);
            } else if (__selectedString.equals("Exit")) {//GEN-LINE:|22-action|5|28-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|22-action|6|28-postAction
                // write post-action user code here
            }//GEN-BEGIN:|22-action|7|22-postAction
        }//GEN-END:|22-action|7|22-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|22-action|8|
    //</editor-fold>//GEN-END:|22-action|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisServerLog ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of thesisServerLog component.
     * @return the initialized component instance
     */
    public Form getThesisServerLog() {
        if (thesisServerLog == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            thesisServerLog = new Form("Server Log");//GEN-BEGIN:|38-getter|1|38-postInit
            thesisServerLog.addCommand(getExitCommand());
            thesisServerLog.addCommand(getSettingsCommand());
            thesisServerLog.addCommand(getAutoModeCommand());
            thesisServerLog.setCommandListener(this);//GEN-END:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return thesisServerLog;
    }
    //</editor-fold>//GEN-END:|38-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisClientMenu ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of thesisClientMenu component.
     * @return the initialized component instance
     */
    public List getThesisClientMenu() {
        if (thesisClientMenu == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            thesisClientMenu = new List("Client Menu", Choice.IMPLICIT);//GEN-BEGIN:|43-getter|1|43-postInit
            thesisClientMenu.append(ThesisConstants.REC3SEC, getDownldIcon());
            thesisClientMenu.append(ThesisConstants.REC5SEC, getDownldIcon());
            thesisClientMenu.append(ThesisConstants.REC10SEC, getDownldIcon());
            thesisClientMenu.addCommand(getExitCommand());
            thesisClientMenu.addCommand(getNotifcnsCommand());
            thesisClientMenu.setCommandListener(this);
            thesisClientMenu.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return thesisClientMenu;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: thesisClientMenuAction ">//GEN-BEGIN:|43-action|0|43-preAction
    /**
     * Performs an action assigned to the selected list element in the thesisClientMenu component.
     */
    public void thesisClientMenuAction() {//GEN-END:|43-action|0|43-preAction
        // enter pre-action user code here
        String __selectedString = getThesisClientMenu().getString(getThesisClientMenu().getSelectedIndex());//GEN-BEGIN:|43-action|1|54-preAction
        if (__selectedString != null) {
            if (__selectedString.equals(ThesisConstants.REC3SEC)) {//GEN-END:|43-action|1|54-preAction
                // write pre-action user code here
                if (clntSettingsFlag[0]) {
                    String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                    thesisClient.sendSMS(msg, defaultSrvrNum);
                    return;
                }
                switchDisplayable(null, getServerNumber());//GEN-LINE:|43-action|2|54-postAction
                // write post-action user code here
            } else if (__selectedString.equals(ThesisConstants.REC5SEC)) {//GEN-LINE:|43-action|3|109-preAction
                // write pre-action user code here
                if (clntSettingsFlag[0]) {
                    String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                    thesisClient.sendSMS(msg, defaultSrvrNum);
                    return;
                }
                switchDisplayable(null, getServerNumber());//GEN-LINE:|43-action|4|109-postAction
                // write post-action user code here
            } else if (__selectedString.equals(ThesisConstants.REC10SEC)) {//GEN-LINE:|43-action|5|110-preAction
                // write pre-action user code here
                if (clntSettingsFlag[0]) {
                    String msg = thesisClientMenu.getString(thesisClientMenu.getSelectedIndex());
                    thesisClient.sendSMS(msg, defaultSrvrNum);
                    return;
                }
                switchDisplayable(null, getServerNumber());//GEN-LINE:|43-action|6|110-postAction
                // write post-action user code here
            }//GEN-BEGIN:|43-action|7|43-postAction
        }//GEN-END:|43-action|7|43-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|43-action|8|
    //</editor-fold>//GEN-END:|43-action|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Send ">//GEN-BEGIN:|48-getter|0|48-preInit
    /**
     * Returns an initiliazed instance of Send component.
     * @return the initialized component instance
     */
    public Command getSend() {
        if (Send == null) {//GEN-END:|48-getter|0|48-preInit
            // write pre-init user code here
            Send = new Command("Send", Command.OK, 0);//GEN-LINE:|48-getter|1|48-postInit
            // write post-init user code here
        }//GEN-BEGIN:|48-getter|2|
        return Send;
    }
    //</editor-fold>//GEN-END:|48-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: serverNumber ">//GEN-BEGIN:|55-getter|0|55-preInit
    /**
     * Returns an initiliazed instance of serverNumber component.
     * @return the initialized component instance
     */
    public TextBox getServerNumber() {
        if (serverNumber == null) {//GEN-END:|55-getter|0|55-preInit
            // write pre-init user code here
            serverNumber = new TextBox("Server Number", null, 100, TextField.PHONENUMBER);//GEN-BEGIN:|55-getter|1|55-postInit
            serverNumber.addCommand(getSend());
            serverNumber.addCommand(getExitCommand());
            serverNumber.setCommandListener(this);//GEN-END:|55-getter|1|55-postInit
            // write post-init user code here
        }//GEN-BEGIN:|55-getter|2|
        return serverNumber;
    }
    //</editor-fold>//GEN-END:|55-getter|2|






    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|80-getter|0|80-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|80-getter|0|80-preInit
            // write pre-init user code here
            backCommand = new Command("Settings", Command.BACK, 0);//GEN-LINE:|80-getter|1|80-postInit
            // write post-init user code here
        }//GEN-BEGIN:|80-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|80-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: saveCommand ">//GEN-BEGIN:|96-getter|0|96-preInit
    /**
     * Returns an initiliazed instance of saveCommand component.
     * @return the initialized component instance
     */
    public Command getSaveCommand() {
        if (saveCommand == null) {//GEN-END:|96-getter|0|96-preInit
            // write pre-init user code here
            saveCommand = new Command("Save", Command.OK, 0);//GEN-LINE:|96-getter|1|96-postInit
            // write post-init user code here
        }//GEN-BEGIN:|96-getter|2|
        return saveCommand;
    }
    //</editor-fold>//GEN-END:|96-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|99-getter|0|99-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand() {
        if (cancelCommand == null) {//GEN-END:|99-getter|0|99-preInit
            // write pre-init user code here
            cancelCommand = new Command("Cancel", Command.CANCEL, 0);//GEN-LINE:|99-getter|1|99-postInit
            // write post-init user code here
        }//GEN-BEGIN:|99-getter|2|
        return cancelCommand;
    }
    //</editor-fold>//GEN-END:|99-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisInfoAlert ">//GEN-BEGIN:|105-getter|0|105-preInit
    /**
     * Returns an initiliazed instance of thesisInfoAlert component.
     * @return the initialized component instance
     */
    public Alert getThesisInfoAlert() {
        if (thesisInfoAlert == null) {//GEN-END:|105-getter|0|105-preInit
            // write pre-init user code here
            thesisInfoAlert = new Alert("Information", null, getInfoIcon(), AlertType.INFO);//GEN-BEGIN:|105-getter|1|105-postInit
            thesisInfoAlert.setTimeout(Alert.FOREVER);//GEN-END:|105-getter|1|105-postInit
            // write post-init user code here
        }//GEN-BEGIN:|105-getter|2|
        return thesisInfoAlert;
    }
    //</editor-fold>//GEN-END:|105-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: thesisErrorAlert ">//GEN-BEGIN:|108-getter|0|108-preInit
    /**
     * Returns an initiliazed instance of thesisErrorAlert component.
     * @return the initialized component instance
     */
    public Alert getThesisErrorAlert() {
        if (thesisErrorAlert == null) {//GEN-END:|108-getter|0|108-preInit
            // write pre-init user code here
            thesisErrorAlert = new Alert("alert", null, getErrorIcon(), null);//GEN-BEGIN:|108-getter|1|108-postInit
            thesisErrorAlert.setTimeout(Alert.FOREVER);//GEN-END:|108-getter|1|108-postInit
            // write post-init user code here
        }//GEN-BEGIN:|108-getter|2|
        return thesisErrorAlert;
    }
    //</editor-fold>//GEN-END:|108-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: serverSettingsForm ">//GEN-BEGIN:|115-getter|0|115-preInit
    /**
     * Returns an initiliazed instance of serverSettingsForm component.
     * @return the initialized component instance
     */
    public Form getServerSettingsForm() {
        if (serverSettingsForm == null) {//GEN-END:|115-getter|0|115-preInit
            // write pre-init user code here
            serverSettingsForm = new Form("Server Settings", new Item[] { getSaveRecFlagCG(), getFnPrfxTextField(), getPushRegCG(), getClientNumTF(), getNotificationsCG() });//GEN-BEGIN:|115-getter|1|115-postInit
            serverSettingsForm.addCommand(getCancelCommand());
            serverSettingsForm.addCommand(getSaveCommand());
            serverSettingsForm.setCommandListener(this);//GEN-END:|115-getter|1|115-postInit
            // write post-init user code here
        }//GEN-BEGIN:|115-getter|2|
        return serverSettingsForm;
    }
    //</editor-fold>//GEN-END:|115-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: notifcnsCommand ">//GEN-BEGIN:|113-getter|0|113-preInit
    /**
     * Returns an initiliazed instance of notifcnsCommand component.
     * @return the initialized component instance
     */
    public Command getNotifcnsCommand() {
        if (notifcnsCommand == null) {//GEN-END:|113-getter|0|113-preInit
            // write pre-init user code here
            notifcnsCommand = new Command("Settings", Command.OK, 0);//GEN-LINE:|113-getter|1|113-postInit
            // write post-init user code here
        }//GEN-BEGIN:|113-getter|2|
        return notifcnsCommand;
    }
    //</editor-fold>//GEN-END:|113-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fnPrfxTextField ">//GEN-BEGIN:|123-getter|0|123-preInit
    /**
     * Returns an initiliazed instance of fnPrfxTextField component.
     * @return the initialized component instance
     */
    public TextField getFnPrfxTextField() {
        if (fnPrfxTextField == null) {//GEN-END:|123-getter|0|123-preInit
            // write pre-init user code here
            fnPrfxTextField = new TextField("Filename Prefix", "ths", 32, TextField.ANY);//GEN-LINE:|123-getter|1|123-postInit
            // write post-init user code here
        }//GEN-BEGIN:|123-getter|2|
        return fnPrfxTextField;
    }
    //</editor-fold>//GEN-END:|123-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: saveRecFlagCG ">//GEN-BEGIN:|124-getter|0|124-preInit
    /**
     * Returns an initiliazed instance of saveRecFlagCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getSaveRecFlagCG() {
        if (saveRecFlagCG == null) {//GEN-END:|124-getter|0|124-preInit
            // write pre-init user code here
            saveRecFlagCG = new ChoiceGroup("Recorded Files", Choice.MULTIPLE);//GEN-BEGIN:|124-getter|1|124-postInit
            saveRecFlagCG.append("Save Recorded Videos", null);
            saveRecFlagCG.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            saveRecFlagCG.setSelectedFlags(new boolean[] { false });//GEN-END:|124-getter|1|124-postInit
            // write post-init user code here
        }//GEN-BEGIN:|124-getter|2|
        return saveRecFlagCG;
    }
    //</editor-fold>//GEN-END:|124-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clientSettingsForm ">//GEN-BEGIN:|128-getter|0|128-preInit
    /**
     * Returns an initiliazed instance of clientSettingsForm component.
     * @return the initialized component instance
     */
    public Form getClientSettingsForm() {
        if (clientSettingsForm == null) {//GEN-END:|128-getter|0|128-preInit
            // write pre-init user code here
            clientSettingsForm = new Form("Client Settings", new Item[] { getDefSrvrTF(), getClntSettingsFlgCG() });//GEN-BEGIN:|128-getter|1|128-postInit
            clientSettingsForm.addCommand(getSaveCommand());
            clientSettingsForm.addCommand(getCancelCommand());
            clientSettingsForm.setCommandListener(this);//GEN-END:|128-getter|1|128-postInit
            // write post-init user code here
        }//GEN-BEGIN:|128-getter|2|
        return clientSettingsForm;
    }
    //</editor-fold>//GEN-END:|128-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clntSettingsFlgCG ">//GEN-BEGIN:|130-getter|0|130-preInit
    /**
     * Returns an initiliazed instance of clntSettingsFlgCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getClntSettingsFlgCG() {
        if (clntSettingsFlgCG == null) {//GEN-END:|130-getter|0|130-preInit
            // write pre-init user code here
            clntSettingsFlgCG = new ChoiceGroup("Server", Choice.MULTIPLE);//GEN-BEGIN:|130-getter|1|130-postInit
            clntSettingsFlgCG.append("Automatically use server number", null);
            clntSettingsFlgCG.setSelectedFlags(new boolean[] { false });//GEN-END:|130-getter|1|130-postInit
            // write post-init user code here
        }//GEN-BEGIN:|130-getter|2|
        return clntSettingsFlgCG;
    }
    //</editor-fold>//GEN-END:|130-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: defSrvrTF ">//GEN-BEGIN:|132-getter|0|132-preInit
    /**
     * Returns an initiliazed instance of defSrvrTF component.
     * @return the initialized component instance
     */
    public TextField getDefSrvrTF() {
        if (defSrvrTF == null) {//GEN-END:|132-getter|0|132-preInit
            // write pre-init user code here
            defSrvrTF = new TextField("Default Server Number", null, 32, TextField.PHONENUMBER);//GEN-LINE:|132-getter|1|132-postInit
            // write post-init user code here
        }//GEN-BEGIN:|132-getter|2|
        return defSrvrTF;
    }
    //</editor-fold>//GEN-END:|132-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: pushRegCG ">//GEN-BEGIN:|137-getter|0|137-preInit
    /**
     * Returns an initiliazed instance of pushRegCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getPushRegCG() {
        if (pushRegCG == null) {//GEN-END:|137-getter|0|137-preInit
            // write pre-init user code here
            pushRegCG = new ChoiceGroup("Auto-startup Settings", Choice.MULTIPLE);//GEN-BEGIN:|137-getter|1|137-postInit
            pushRegCG.append("Allow auto-startup", null);
            pushRegCG.setSelectedFlags(new boolean[] { false });//GEN-END:|137-getter|1|137-postInit
            // write post-init user code here
        }//GEN-BEGIN:|137-getter|2|
        return pushRegCG;
    }
    //</editor-fold>//GEN-END:|137-getter|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clientNumTF ">//GEN-BEGIN:|142-getter|0|142-preInit
    /**
     * Returns an initiliazed instance of clientNumTF component.
     * @return the initialized component instance
     */
    public TextField getClientNumTF() {
        if (clientNumTF == null) {//GEN-END:|142-getter|0|142-preInit
            // write pre-init user code here
            clientNumTF = new TextField("Auto-startup Client Number", null, 32, TextField.PHONENUMBER);//GEN-LINE:|142-getter|1|142-postInit
            // write post-init user code here
        }//GEN-BEGIN:|142-getter|2|
        return clientNumTF;
    }
    //</editor-fold>//GEN-END:|142-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: serverIcon ">//GEN-BEGIN:|143-getter|0|143-preInit
    /**
     * Returns an initiliazed instance of serverIcon component.
     * @return the initialized component instance
     */
    public Image getServerIcon() {
        if (serverIcon == null) {//GEN-END:|143-getter|0|143-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|143-getter|1|143-@java.io.IOException
                serverIcon = Image.createImage("/32x32/computer_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|143-getter|1|143-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|143-getter|2|143-postInit
            // write post-init user code here
        }//GEN-BEGIN:|143-getter|3|
        return serverIcon;
    }
    //</editor-fold>//GEN-END:|143-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: clientIcon ">//GEN-BEGIN:|144-getter|0|144-preInit
    /**
     * Returns an initiliazed instance of clientIcon component.
     * @return the initialized component instance
     */
    public Image getClientIcon() {
        if (clientIcon == null) {//GEN-END:|144-getter|0|144-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|144-getter|1|144-@java.io.IOException
                clientIcon = Image.createImage("/32x32/home_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|144-getter|1|144-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|144-getter|2|144-postInit
            // write post-init user code here
        }//GEN-BEGIN:|144-getter|3|
        return clientIcon;
    }
    //</editor-fold>//GEN-END:|144-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: notifcIcon ">//GEN-BEGIN:|145-getter|0|145-preInit
    /**
     * Returns an initiliazed instance of notifcIcon component.
     * @return the initialized component instance
     */
    public Image getNotifcIcon() {
        if (notifcIcon == null) {//GEN-END:|145-getter|0|145-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|145-getter|1|145-@java.io.IOException
                notifcIcon = Image.createImage("/32x32/direction_left_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|145-getter|1|145-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|145-getter|2|145-postInit
            // write post-init user code here
        }//GEN-BEGIN:|145-getter|3|
        return notifcIcon;
    }
    //</editor-fold>//GEN-END:|145-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitIcon ">//GEN-BEGIN:|146-getter|0|146-preInit
    /**
     * Returns an initiliazed instance of exitIcon component.
     * @return the initialized component instance
     */
    public Image getExitIcon() {
        if (exitIcon == null) {//GEN-END:|146-getter|0|146-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|146-getter|1|146-@java.io.IOException
                exitIcon = Image.createImage("/32x32/user_delete_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|146-getter|1|146-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|146-getter|2|146-postInit
            // write post-init user code here
        }//GEN-BEGIN:|146-getter|3|
        return exitIcon;
    }
    //</editor-fold>//GEN-END:|146-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: errorIcon ">//GEN-BEGIN:|147-getter|0|147-preInit
    /**
     * Returns an initiliazed instance of errorIcon component.
     * @return the initialized component instance
     */
    public Image getErrorIcon() {
        if (errorIcon == null) {//GEN-END:|147-getter|0|147-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|147-getter|1|147-@java.io.IOException
                errorIcon = Image.createImage("/32x32/notification_error_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|147-getter|1|147-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|147-getter|2|147-postInit
            // write post-init user code here
        }//GEN-BEGIN:|147-getter|3|
        return errorIcon;
    }
    //</editor-fold>//GEN-END:|147-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: downldIcon ">//GEN-BEGIN:|148-getter|0|148-preInit
    /**
     * Returns an initiliazed instance of downldIcon component.
     * @return the initialized component instance
     */
    public Image getDownldIcon() {
        if (downldIcon == null) {//GEN-END:|148-getter|0|148-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|148-getter|1|148-@java.io.IOException
                downldIcon = Image.createImage("/32x32/clock_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|148-getter|1|148-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|148-getter|2|148-postInit
            // write post-init user code here
        }//GEN-BEGIN:|148-getter|3|
        return downldIcon;
    }
    //</editor-fold>//GEN-END:|148-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: infoIcon ">//GEN-BEGIN:|149-getter|0|149-preInit
    /**
     * Returns an initiliazed instance of infoIcon component.
     * @return the initialized component instance
     */
    public Image getInfoIcon() {
        if (infoIcon == null) {//GEN-END:|149-getter|0|149-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|149-getter|1|149-@java.io.IOException
                infoIcon = Image.createImage("/32x32/message_21x21.png");
            } catch (java.io.IOException e) {//GEN-END:|149-getter|1|149-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|149-getter|2|149-postInit
            // write post-init user code here
        }//GEN-BEGIN:|149-getter|3|
        return infoIcon;
    }
    //</editor-fold>//GEN-END:|149-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|150-getter|0|150-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|150-getter|0|150-preInit
            // write pre-init user code here
            okCommand = new Command("Start", Command.OK, 0);//GEN-LINE:|150-getter|1|150-postInit
            // write post-init user code here
        }//GEN-BEGIN:|150-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|150-getter|2|

    //</editor-fold>
    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashImg ">//GEN-BEGIN:|159-getter|0|159-preInit
    /**
     * Returns an initiliazed instance of splashImg component.
     * @return the initialized component instance
     */
    public Image getSplashImg() {
        if (splashImg == null) {//GEN-END:|159-getter|0|159-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|159-getter|1|159-@java.io.IOException
                splashImg = Image.createImage("/disc.png");
            } catch (java.io.IOException e) {//GEN-END:|159-getter|1|159-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|159-getter|2|159-postInit
            // write post-init user code here
        }//GEN-BEGIN:|159-getter|3|
        return splashImg;
    }
    //</editor-fold>//GEN-END:|159-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: settingsCommand ">//GEN-BEGIN:|160-getter|0|160-preInit
    /**
     * Returns an initiliazed instance of settingsCommand component.
     * @return the initialized component instance
     */
    public Command getSettingsCommand() {
        if (settingsCommand == null) {//GEN-END:|160-getter|0|160-preInit
            // write pre-init user code here
            settingsCommand = new Command("Settings", Command.OK, 2);//GEN-LINE:|160-getter|1|160-postInit
            // write post-init user code here
        }//GEN-BEGIN:|160-getter|2|
        return settingsCommand;
    }
    //</editor-fold>//GEN-END:|160-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: notificationsCG ">//GEN-BEGIN:|164-getter|0|164-preInit
    /**
     * Returns an initiliazed instance of notificationsCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getNotificationsCG() {
        if (notificationsCG == null) {//GEN-END:|164-getter|0|164-preInit
            // write pre-init user code here
            notificationsCG = new ChoiceGroup("Notifications", Choice.MULTIPLE);//GEN-BEGIN:|164-getter|1|164-postInit
            notificationsCG.append("Request received", null);
            notificationsCG.append("MMS sent", null);
            notificationsCG.append("Server errors", null);
            notificationsCG.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|164-getter|1|164-postInit
            // write post-init user code here
        }//GEN-BEGIN:|164-getter|2|
        return notificationsCG;
    }
    //</editor-fold>//GEN-END:|164-getter|2|





    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: BusyForm ">//GEN-BEGIN:|174-getter|0|174-preInit
    /**
     * Returns an initiliazed instance of BusyForm component.
     * @return the initialized component instance
     */
    public Form getBusyForm() {
        if (BusyForm == null) {//GEN-END:|174-getter|0|174-preInit
            // write pre-init user code here
            BusyForm = new Form("Working..", new Item[] { getBusyGauge() });//GEN-LINE:|174-getter|1|174-postInit
            // write post-init user code here
        }//GEN-BEGIN:|174-getter|2|
        return BusyForm;
    }
    //</editor-fold>//GEN-END:|174-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: busyGauge ">//GEN-BEGIN:|175-getter|0|175-preInit
    /**
     * Returns an initiliazed instance of busyGauge component.
     * @return the initialized component instance
     */
    public Gauge getBusyGauge() {
        if (busyGauge == null) {//GEN-END:|175-getter|0|175-preInit
            // write pre-init user code here
            busyGauge = new Gauge("Busy..", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);//GEN-BEGIN:|175-getter|1|175-postInit
            busyGauge.setLayout(ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER);//GEN-END:|175-getter|1|175-postInit
            // write post-init user code here
        }//GEN-BEGIN:|175-getter|2|
        return busyGauge;
    }
    //</editor-fold>//GEN-END:|175-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: autoModeForm ">//GEN-BEGIN:|178-getter|0|178-preInit
    /**
     * Returns an initiliazed instance of autoModeForm component.
     * @return the initialized component instance
     */
    public Form getAutoModeForm() {
        if (autoModeForm == null) {//GEN-END:|178-getter|0|178-preInit
            // write pre-init user code here
            autoModeForm = new Form("Auto-mode Settings", new Item[] { getTimeIntervalGauge(), getAutoModeDurationCG(), getAutoModeClientNumTF() });//GEN-BEGIN:|178-getter|1|178-postInit
            autoModeForm.addCommand(getCancelCommand());
            autoModeForm.addCommand(getOkCommand());
            autoModeForm.setCommandListener(this);//GEN-END:|178-getter|1|178-postInit
            // write post-init user code here
            autoModeForm.setItemStateListener(timeGaugeListener);
        }//GEN-BEGIN:|178-getter|2|
        return autoModeForm;
    }
    //</editor-fold>//GEN-END:|178-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: autoModeCommand ">//GEN-BEGIN:|176-getter|0|176-preInit
    /**
     * Returns an initiliazed instance of autoModeCommand component.
     * @return the initialized component instance
     */
    public Command getAutoModeCommand() {
        if (autoModeCommand == null) {//GEN-END:|176-getter|0|176-preInit
            // write pre-init user code here
            autoModeCommand = new Command("Auto-mode", Command.OK, 1);//GEN-LINE:|176-getter|1|176-postInit
            // write post-init user code here
        }//GEN-BEGIN:|176-getter|2|
        return autoModeCommand;
    }
    //</editor-fold>//GEN-END:|176-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: timeIntervalGauge ">//GEN-BEGIN:|187-getter|0|187-preInit
    /**
     * Returns an initiliazed instance of timeIntervalGauge component.
     * @return the initialized component instance
     */
    public Gauge getTimeIntervalGauge() {
        if (timeIntervalGauge == null) {//GEN-END:|187-getter|0|187-preInit
            // write pre-init user code here
            timeIntervalGauge = new Gauge(ThesisConstants.getTimeIntervalStr(5), true, 9, 5);//GEN-LINE:|187-getter|1|187-postInit
            // write post-init user code here
        }//GEN-BEGIN:|187-getter|2|
        return timeIntervalGauge;
    }
    //</editor-fold>//GEN-END:|187-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: autoServerWaitScreen ">//GEN-BEGIN:|189-getter|0|189-preInit
    /**
     * Returns an initiliazed instance of autoServerWaitScreen component.
     * @return the initialized component instance
     */
    public WaitScreen getAutoServerWaitScreen() {
        if (autoServerWaitScreen == null) {//GEN-END:|189-getter|0|189-preInit
            // write pre-init user code here
            autoServerWaitScreen = new WaitScreen(getDisplay());//GEN-BEGIN:|189-getter|1|189-postInit
            autoServerWaitScreen.setTitle("Automated Server Mode");
            autoServerWaitScreen.addCommand(getExitCommand());
            autoServerWaitScreen.addCommand(getBackCommand());
            autoServerWaitScreen.setCommandListener(this);
            autoServerWaitScreen.setImage(randomWallpaper());
            autoServerWaitScreen.setText("00:00");
            autoServerWaitScreen.setTask(getAutoServerTask());//GEN-END:|189-getter|1|189-postInit
            // write post-init user code here
        }//GEN-BEGIN:|189-getter|2|
        return autoServerWaitScreen;
    }
    //</editor-fold>//GEN-END:|189-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: autoServerTask ">//GEN-BEGIN:|192-getter|0|192-preInit
    /**
     * Returns an initiliazed instance of autoServerTask component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getAutoServerTask() {
        if (autoServerTask == null) {//GEN-END:|192-getter|0|192-preInit
            // write pre-init user code here
            autoServerTask = new SimpleCancellableTask();//GEN-BEGIN:|192-getter|1|192-execute
            autoServerTask.setExecutable(new org.netbeans.microedition.util.Executable() {
                public void execute() throws Exception {//GEN-END:|192-getter|1|192-execute
                    // write task-execution user code here
                    int sec = 0;
                    int secTask = ThesisConstants.getTimeIntervalSec(getTimeIntervalGauge().getValue());
                    int min = -1;
                    int duration = ThesisConstants.getChoiceDurationFromCGIdx(getAutoModeDurationCG().getSelectedIndex());
                    while (autoTaskRun) {
                        Thread.sleep(1000L);
                        sec++;
                        min = sec/60;
                        autoServerWaitScreen.setText( (min < 10 ? "0" : "") + min +
                                ":" + (sec%60 < 10 ? "0" : "") + sec%60);
                        if (sec % secTask == 0) {
                            autoServerWaitScreen.setText("Recording for " + duration + " seconds");
                            thesisServer.doRecord(duration);
                        }
                    }
                }//GEN-BEGIN:|192-getter|2|192-postInit
            });//GEN-END:|192-getter|2|192-postInit
            // write post-init user code here
        }//GEN-BEGIN:|192-getter|3|
        return autoServerTask;
    }
    //</editor-fold>//GEN-END:|192-getter|3|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: autoModeDurationCG ">//GEN-BEGIN:|194-getter|0|194-preInit
    /**
     * Returns an initiliazed instance of autoModeDurationCG component.
     * @return the initialized component instance
     */
    public ChoiceGroup getAutoModeDurationCG() {
        if (autoModeDurationCG == null) {//GEN-END:|194-getter|0|194-preInit
            // write pre-init user code here
            autoModeDurationCG = new ChoiceGroup("Duration", Choice.EXCLUSIVE);//GEN-BEGIN:|194-getter|1|194-postInit
            autoModeDurationCG.append("3 Seconds", null);
            autoModeDurationCG.append("5 Seconds", null);
            autoModeDurationCG.append("10 Seconds", null);
            autoModeDurationCG.setSelectedFlags(new boolean[] { true, false, false });//GEN-END:|194-getter|1|194-postInit
            // write post-init user code here
        }//GEN-BEGIN:|194-getter|2|
        return autoModeDurationCG;
    }
    //</editor-fold>//GEN-END:|194-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|199-getter|0|199-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|199-getter|0|199-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|199-getter|1|199-postInit
            // write post-init user code here
        }//GEN-BEGIN:|199-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|199-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image2 ">//GEN-BEGIN:|206-getter|0|206-preInit
    /**
     * Returns an initiliazed instance of image2 component.
     * @return the initialized component instance
     */
    public Image getImage2() {
        if (image2 == null) {//GEN-END:|206-getter|0|206-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|206-getter|1|206-@java.io.IOException
                image2 = Image.createImage("/autumn-arctic-landscape_7906.jpg");
            } catch (java.io.IOException e) {//GEN-END:|206-getter|1|206-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|206-getter|2|206-postInit
            // write post-init user code here
        }//GEN-BEGIN:|206-getter|3|
        return image2;
    }
    //</editor-fold>//GEN-END:|206-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|207-getter|0|207-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|207-getter|0|207-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|207-getter|1|207-@java.io.IOException
                image1 = Image.createImage("/Landscape-Decoration-Blanc-Carrare.jpg");
            } catch (java.io.IOException e) {//GEN-END:|207-getter|1|207-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|207-getter|2|207-postInit
            // write post-init user code here
        }//GEN-BEGIN:|207-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|207-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image3 ">//GEN-BEGIN:|208-getter|0|208-preInit
    /**
     * Returns an initiliazed instance of image3 component.
     * @return the initialized component instance
     */
    public Image getImage3() {
        if (image3 == null) {//GEN-END:|208-getter|0|208-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|208-getter|1|208-@java.io.IOException
                image3 = Image.createImage("/landscape-design.jpg");
            } catch (java.io.IOException e) {//GEN-END:|208-getter|1|208-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|208-getter|2|208-postInit
            // write post-init user code here
        }//GEN-BEGIN:|208-getter|3|
        return image3;
    }
    //</editor-fold>//GEN-END:|208-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: autoModeClientNumTF ">//GEN-BEGIN:|209-getter|0|209-preInit
    /**
     * Returns an initiliazed instance of autoModeClientNumTF component.
     * @return the initialized component instance
     */
    public TextField getAutoModeClientNumTF() {
        if (autoModeClientNumTF == null) {//GEN-END:|209-getter|0|209-preInit
            // write pre-init user code here
            autoModeClientNumTF = new TextField("Client Number", null, 32, TextField.PHONENUMBER);//GEN-LINE:|209-getter|1|209-postInit
            // write post-init user code here
        }//GEN-BEGIN:|209-getter|2|
        return autoModeClientNumTF;
    }
    //</editor-fold>//GEN-END:|209-getter|2|

    private boolean autoTaskRun = false;

    ItemStateListener timeGaugeListener = new ItemStateListener() {

        public void itemStateChanged(Item item) {
            if (item == timeIntervalGauge) {
                timeIntervalGauge.setLabel(ThesisConstants.getTimeIntervalStr(
                        timeIntervalGauge.getValue()));
            }
        }
    };

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        cleanConnections();
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        cleanConnections();
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

    public void showErrorAlert(String message) {
        getThesisErrorAlert().setString(message);
        switchDisplayable(getThesisErrorAlert(), Display.getDisplay(this).getCurrent());
    }

    public void showErrorAlert(Throwable th) {
        th.printStackTrace();
        showErrorAlert(th.getMessage());
    }

    public void showInfoAlert(String message) {
        getThesisInfoAlert().setString(message);
        switchDisplayable(getThesisInfoAlert(), Display.getDisplay(this).getCurrent());
    }

    private void cleanConnections() {
        if (thesisServer != null) {
            thesisServer.destroy();
            thesisServer = null;
        }
        if (thesisClient != null) {
            thesisClient.destroy();
            thesisClient = null;
        }
        System.gc();
    }

    private void saveSrvrSettings() throws Exception {
        // validation
        // Push reg Settigs
        boolean[] pushregflg = new boolean[1];
        getPushRegCG().getSelectedFlags(pushregflg);
        try {
            String cons[] = PushRegistry.listConnections(false);
            for (int i = 0; i < cons.length; i++) {
                PushRegistry.unregisterConnection(cons[i]);
            }
        } catch (Throwable t) {
            logServer(t);
        }
        if (pushregflg[0]) {

            if (clientNumTF.getString() == null ||
                    clientNumTF.getString().equals(ThesisConstants.EMPTY)) {
                throw new Exception("Enter a client number");
            }
            clientNum = clientNumTF.getString();
            if (clientNum.startsWith("0")) {
                clientNum = "+63" + clientNum.substring(1);
            }
            pushRegFlag = pushregflg[0];
            try {
                PushRegistry.registerConnection("sms://:" + ThesisConstants.SMSPORT, "app.ThesisMIDlet", clientNum);
            } catch (Throwable t) {
                logServer(t);
            }
        } else {
            clientNum = "";
        }
        thesisSrvrSettings.put("clntnum", clientNum);
        thesisSrvrSettings.put("pshregflg", pushRegFlag + "");

        // notifcations settingsFlag
        getNotificationsCG().getSelectedFlags(srvrSettingsFlag);
        for (int i = 0; i < srvrSettingsFlag.length; i++) {
            boolean b = srvrSettingsFlag[i];
            thesisSrvrSettings.put(i + "", b + "");
        }
        // filename prefix
        fileNamePrefix = getFnPrfxTextField().getString();
        fileNamePrefix.trim(); // TODO: error trap invalid filenames
        thesisSrvrSettings.put("fnprfx", fileNamePrefix);
        // save recorded file flag
        boolean[] svrcsflg = new boolean[1];
        getSaveRecFlagCG().getSelectedFlags(svrcsflg);
        saveRecsFlag = svrcsflg[0];
        thesisSrvrSettings.put("svrcsflg", saveRecsFlag + "");

        try {
            thesisSrvrSettings.save();
        } catch (Throwable ex) {
            logServer(ex);
        }
    }

    private void loadSrvrSettings() {
        if (!thesisSrvrSettings.isInitial()) {
            for (int i = 0; i < srvrSettingsFlag.length; i++) {
                srvrSettingsFlag[i] = thesisSrvrSettings.get(ThesisConstants.EMPTY + i).equals(ThesisConstants.BOOLEAN_T) ? true : false;
            }
            getNotificationsCG().setSelectedFlags(srvrSettingsFlag);
            fileNamePrefix = thesisSrvrSettings.get("fnprfx");
            getFnPrfxTextField().setString(fileNamePrefix);
            saveRecsFlag = thesisSrvrSettings.get("svrcsflg").equals(ThesisConstants.BOOLEAN_T) ? true : false;
            getSaveRecFlagCG().setSelectedIndex(0, saveRecsFlag);
            pushRegFlag = thesisSrvrSettings.get("pshregflg").equals(ThesisConstants.BOOLEAN_T) ? true : false;
            getPushRegCG().setSelectedIndex(0, pushRegFlag);
            clientNum = thesisSrvrSettings.get("clntnum");
            getClientNumTF().setString(clientNum);
        }
    }

    private void loadClntSettings() {
        if (!thesisClntSettings.isInitial()) {
            for (int i = 0; i < clntSettingsFlag.length; i++) {
                clntSettingsFlag[i] = thesisClntSettings.get(ThesisConstants.EMPTY + i).equals(ThesisConstants.BOOLEAN_T) ? true : false;
            }
            getClntSettingsFlgCG().setSelectedFlags(clntSettingsFlag);
            defaultSrvrNum = thesisClntSettings.get("defsrvrnum");
            getDefSrvrTF().setString(defaultSrvrNum);
        }
    }

    private void saveClntSettings() {
        // settingsFlag
        getClntSettingsFlgCG().getSelectedFlags(clntSettingsFlag);
        for (int i = 0; i < clntSettingsFlag.length; i++) {
            boolean b = clntSettingsFlag[i];
            thesisClntSettings.put(i + "", b + "");
        }
        defaultSrvrNum = getDefSrvrTF().getString();
        thesisClntSettings.put("defsrvrnum", defaultSrvrNum);

        try {
            thesisClntSettings.save();
        } catch (Throwable ex) {
            showErrorAlert(ex);
        }
    }

    // SETTINGS GETTERS
    public boolean[] getClntSettingsFlag() {
        return clntSettingsFlag;
    }

    public String getDefaultSrvrNum() {
        return defaultSrvrNum;
    }

    public String getFileNamePrefix() {
        return fileNamePrefix;
    }

    public boolean isSaveRecsFlag() {
        return saveRecsFlag;
    }

    public boolean[] getSrvrSettingsFlag() {
        return srvrSettingsFlag;
    }

    public boolean isServerCapable() {
        try {
            Class.forName("javax.wireless.messaging.MultipartMessage");
        } catch (ClassNotFoundException ex) {
            return false;
        }
        return true;
    }

    public MessageConnection getSMSServer() {
        return SMSServer;
    }

    public boolean isStartedByReq() {
        return startedByReq;
    }

    public void setStartedByReq(boolean startedByReq) {
        this.startedByReq = startedByReq;
    }

    private void logServer(Throwable t) {
        logServer(t.getMessage());
    }

    private void logServer(String s) {
        StringItem si = new StringItem(null, s);
        si.setLayout(Item.LAYOUT_2 | Item.LAYOUT_LEFT | Item.LAYOUT_NEWLINE_AFTER);
        thesisServerLog.append(s);
    }

    private void initServer() {
        cleanConnections();
        if (isServerCapable()) {
            try {
                thesisServer = new ThesisServer(thesisServerLog, this);
            } catch (Throwable t) {
                logServer("ERROR: " + t.getMessage());
            }
        } else {
            logServer("Sorry, your mobile phone is not capable as server");
        }
    }

    private Image randomWallpaper(){
        Random r = new Random();
        int rand = r.nextInt();
        switch(Math.abs(rand) % 3){
            case 0: return getImage1();
            case 1: return getImage2();
            case 2: return getImage3();
        }
        return null;
    }
}
